<?php
// 应用公共文件
function now(){
    return date('Y-m-d H:i:s');
}
function doCurlPostRequest($url = '',Array $data = array())
{
    $data_string = json_encode($data,JSON_UNESCAPED_UNICODE);
    // $data_string = $data;
    $curl_con = curl_init();
    curl_setopt($curl_con, CURLOPT_URL,$url);
    curl_setopt($curl_con, CURLOPT_HEADER, false);
    curl_setopt($curl_con, CURLOPT_POST, true);
    curl_setopt($curl_con, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl_con, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($curl_con, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
    );
    curl_setopt($curl_con, CURLOPT_POSTFIELDS, $data_string);
    $res = curl_exec($curl_con);
    curl_close($curl_con);
    return $res;
}
function curl($url, $header,$is_post = true,$data = []){
    // $useragent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36';
    // 创建一个cURL资源
    $ch = curl_init();
    // 设置cURL选项
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, $is_post);
    curl_setopt($ch, CURLOPT_URL, $url);          // 设置目标网页的URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将返回的内容作为字符串返回，而不是直接输出
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//禁止 cURL 验证对等证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//是否检测服务器的域名与证书上的是否一致
    curl_setopt($ch, CURLOPT_ENCODING, "" );
    curl_setopt($ch, CURLOPT_AUTOREFERER, true );
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5 );
    curl_setopt($ch, CURLOPT_TIMEOUT, 5 );
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5 );
    if(!empty($data)){
        $post = http_build_query($data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    // curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    // 执行cURL请求
    $response = curl_exec($ch);
    // 关闭cURL资源
    curl_close($ch);
    // 检查是否有错误发生
    if ($response === false) {
        $error = curl_error($ch);
        return ['error' => $error];
    } else {
        // 处理返回的内容
        return $response;
    }
}

function isValidFileExtension($filename, $extensions) {
    // 构建正则表达式模式，例如：(\.jpg$|\.png$|\.gif$)
    $pattern = '/\.(?:' . implode('|', array_map('preg_quote', $extensions)) . ')$/i';

    // 使用 preg_match 进行匹配
    if (preg_match($pattern, $filename)) {
        return true;
    }

    return false;
}

function compareByPateDesc($a, $b) {
    if ($a['pate'] == $b['pate']) {
        return 0;
    }
    return ($a['pate'] > $b['pate']) ? -1 : 1;
}